﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MyTech.CodeGenerator.Utils
{
    public class FileUtil
    {
        public static bool SaveCode(string fileFullName, string content)
        {
            try
            {
                string[] sArray = Regex.Split(content, Environment.NewLine, RegexOptions.IgnoreCase);
                List<String> sResult = new List<string>();

                bool isFirstBlank = true;
                foreach (string s in sArray)
                {
                    if (string.IsNullOrWhiteSpace(s) && isFirstBlank)
                    {
                        
                    }
                    else
                    {
                        sResult.Add(s);
                        isFirstBlank = false;
                    }
                }
                FileInfo fi = new FileInfo(fileFullName);

                if (File.Exists(fileFullName))
                {
                    File.Delete(fileFullName);
                }
                else
                {
                    string dirName = fi.DirectoryName;
                    bool hasDir = Directory.Exists(dirName);
                    if (!hasDir)
                    {
                        Directory.CreateDirectory(dirName);
                    }
                }

                //UTF-8 without BOM:
                var utf8WithoutBom = new System.Text.UTF8Encoding(false);
                //BOM头的代码：
                //var utf8WithBom = new System.Text.UTF8Encoding(true);  // 用true来指定包含bom
                File.WriteAllLines(fileFullName, sResult, utf8WithoutBom);

            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 清空目录下所有，但目录仍保留
        /// </summary>
        /// <param name="Path"></param>
        public static Boolean ClearDirectory(string Path)
        {
            if (!IsExistDirectory(Path))
            {
                return true;
            }


            try
            {
                string[] DirectoryList = Directory.GetDirectories(Path);
                string[] FileList = Directory.GetFiles(Path);

                foreach (string file in FileList)
                {
                    File.Delete(file);
                }
                foreach (string directory in DirectoryList)
                {
                    Directory.Delete(directory, true);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #region 创建一个目录
        /// <summary>
        /// 创建一个目录
        /// </summary>
        /// <param name="directoryPath">目录的绝对路径</param>
        public static void CreateDirectory(string directoryPath)
        {
            //如果目录不存在则创建该目录
            if (!IsExistDirectory(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
        }
        #endregion

        #region 检测指定目录是否存在
        /// <summary>
        /// 检测指定目录是否存在
        /// </summary>
        /// <param name="directoryPath">目录的绝对路径</param>         
        public static bool IsExistDirectory(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }
        #endregion

    }
}
